package ch01;

import java.lang.reflect.Field;

/**
 * @author yang
 * @date 2024-04-02
 */
public class Student {
    private String name;
    @Age()
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws Exception{
        Class<Student> studentClass = Student.class;
        Field declaredField = studentClass.getDeclaredField("age");
        Age annotation = declaredField.getAnnotation(Age.class);
        if (annotation.value() > age){
            System.out.println("年龄录入失败，必须大于"+annotation.value()+"岁！");
        }else{
            System.out.println("输入成功！");
            this.age = age;
        }
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }
}
